"use strict";

const Controller = require("egg").Controller;

class oldHouseController extends Controller {
  //列表查看
  async oldHouseList() {
    // console.log(user);
    const { ctx } = this;
    const { pageIndex, pageSize } = ctx.request.body;
    const sqlData = await ctx.service.oldHouse.oldHouseList(
      pageIndex,
      pageSize
    );
    ctx.body = {
      code: 200,
      list: sqlData.res,
      total: sqlData.total.length,
      //试验一下
      // total: sqlData.length,
    };
  }
  // 添加
  async oldHouseAdd() {
    const { ctx } = this;
    // console.log(ctx.request.body);
    const {
      name,
      word,
      region,
      mianji,
      price,
      HouseType,
      type,
      company,
      RoleName,
      phone,
      flag,
      names,
      metro,
      num,
      ip,
      time,
    } = ctx.request.body;
    await ctx.service.oldHouse.oldHouseAdd(
      name,
      word,
      region,
      mianji,
      price,
      type,
      company, //公司
      RoleName, //管理员
      HouseType, //几室几厅
      phone, //电话
      flag, //开关
      names, //姓名
      metro, //地铁线
      num, //排序
      ip,
      time
    );
    ctx.body = {
      code: 200,
      msg: "添加成功",
    };
  }
  // 删除
  async oldHouseDel() {
    const { ctx } = this;
    // console.log(ctx.request, "123");
    const { id } = ctx.request.body;
    await ctx.service.oldHouse.oldHouseDel(id);
    ctx.body = {
      code: 200,
      msg: "删除成功",
    };
  }
  //编辑
  async oldHouseSet() {
    const { ctx } = this;
    const obj = {};
    // const { id, name, word, region, mianji, price, houseType, type } =
    //   ctx.request.body;
    const objValue = Object.values(ctx.request.body); //取出对象中的值组成数组
    const objkeys = Object.keys(ctx.request.body); //取出对象的键名
    objkeys.forEach((item, index) => {
      if (objValue[index]) {
        obj[item] = objValue[index];
      }
    });
    await ctx.service.oldHouse.oldHouseSet(obj);
    ctx.body = {
      code: 200,
      msg: "编辑成功",
    };
  }
}

module.exports = oldHouseController;
